layui.define(['element', 'api', 'fast_config', 'utils'], function (exports) {
    "use strict";

    /**
     * Button component
     * */
    var MOD_NAME = 'domain_design_index',
        $ = layui.jquery,
        rowIndex;

    var domain_design_index = function (opt) {
        this.option = opt;
    };

    var initFieldDate = function (option) {
        option.fields = {};
    }

    /**
     * Button start loading
     * */
    domain_design_index.prototype.render = function (opt) {

        var option = {
            elem: opt.elem,
            initData: opt.initData,
            // businFields: opt.businFields,
            grid: null
        }

        initFieldDate(option);

        var grid;
        var metaName;

        var types = [
            {
                "id": "normal",
                "value": "normal",
                "name": "常规"
            },
            {
                "id": "unipue",
                "value": "unipue",
                "name": "唯一"
            }
        ];

        // var businFields = [];
        // if (option.businFields) {
        //     businFields = option.businFields;
        // }

        var obj = {
            hwrap: false,
            virtualX: true,
            rowBorders: false,
            trackModel: {
                on: true
            }, //to turn on the track changes.
            selectionModel: {
                type: null
            },
            toolbar: {
                items: [{
                    type: 'button',
                    icon: 'ui-icon-plus',
                    label: '',
                    listener: function () {
                        let i = grid.getData().length + 1;
                        let rowData = {
                            indexName: "字段" + i,
                            fieldName: "field" + i,
                            // fieldName: null,
                            indexType: "normal"
                        }; //default row
                        let rowIndx = grid.addRow({
                            rowData: rowData,
                            checkEditable: true
                        });
                        grid.goToPage({
                            rowIndx: rowIndx
                        });
                        grid.editFirstCellInRow({
                            rowIndx: rowIndx
                        });
                    }
                },
                    {
                        type: 'button',
                        icon: 'ui-icon-close',
                        label: '',
                        listener: function () {
                            //debugger
                            if (rowIndex != null) {
                                layer.confirm('确认删除选中数据？', {
                                    title: "删除字段",
                                    btn: ['确认', '取消'] //按钮
                                }, function () {
                                    let selarray = grid.selection({
                                            type: 'row',
                                            method: 'getSelection'
                                        }),
                                        checked = [];
                                    for (var i = 0, len = selarray.length; i < len; i++) {
                                        checked.push({
                                            rowIndx: selarray[i].rowIndx
                                        });
                                    }
                                    grid.deleteRow({
                                        rowList: checked
                                    });

                                    rowIndex = null;
                                    layer.closeAll();
                                    layer.msg('操作成功');
                                }, function () {
                                    layer.closeAll();
                                });
                            } else {
                                layer.alert('请先选择行！');
                            }
                        }
                    }
                ]
            },
            scrollModel: {
                autoFit: true
            },
            selectChange: function (evt, ui) {
                console.log('selectChange', ui);
                // debugger;
                let address = ui.selection.address();
                rowIndex = address;
                // $("#select_change_div").html(JSON.stringify(address));
            },
            swipeModel: {
                on: false,
                type: null
            },
            editor: {
                select: true
            },
            bootstrap: {
                on: false
            },
            height: 'flex',
            showTitle: false,
            title: "<b>" + metaName + "</b>",
            history: function (evt, ui) {
                var $tb = this.toolbar();
                if (ui.canUndo != null) {
                    $("button.changes", $tb).button("option", {
                        disabled: !ui.canUndo
                    });
                }
                if (ui.canRedo != null) {
                    $("button:contains('Redo')", $tb).button("option", "disabled", !ui.canRedo);
                }
                $("button:contains('Undo')", $tb).button("option", {
                    label: 'Undo (' + ui.num_undo + ')'
                });
                $("button:contains('Redo')", $tb).button("option", {
                    label: 'Redo (' + ui.num_redo + ')'
                });
            },
            colModel: [
                {
                    dataIndx: "state",
                    maxWidth: 30,
                    minWidth: 30,
                    align: "center",
                    resizable: false,
                    type: 'checkBoxSelection',
                    cls: 'ui-state-default',
                    sortable: false,
                    editor: false,
                    dataType: 'bool',
                    cb: {
                        all: true,
                        header: true,
                        select: true
                    }
                },
                {
                    title: "名称",
                    width: 200,
                    dataType: "string",
                    dataIndx: "indexName",
                    validations: [{
                        type: 'minLen',
                        value: "1",
                        msg: "必填"
                    }]
                },
                {
                    title: "字段名",
                    width: 200,
                    dataType: "string",
                    dataIndx: "fieldName",
                    validations: [{
                        type: 'minLen',
                        value: "1",
                        msg: "必填"
                    }]
                },
                // {
                //     title: "字段",
                //     width: 200,
                //     dataType: "string",
                //     dataIndx: "fieldName",
                //     editor: {
                //         type: "select",
                //         labelIndx: "fieldName",
                //         valueIndx: "fieldName",
                //         prepend: {
                //             "": ""
                //         },
                //         options: businFields
                //     },
                //     validations: [{ type: 'minLen', value: "1", msg: "必填"}],
                //     render: function (ui) {
                //         var rowData = ui.rowData,
                //             dataIndx = ui.dataIndx;
                //
                //         rowData.pq_cellcls = rowData.pq_cellcls || {};
                //
                //         let name = null;
                //         $.each(businFields, function (index, obj) {
                //             if (rowData[dataIndx] == obj.id) {
                //                 name = obj.fieldName
                //                 return true;
                //             }
                //         });
                //         return name;
                //     }
                // },
                {
                    title: "类别",
                    width: 220,
                    dataType: "string",
                    dataIndx: "indexType",
                    editor: {
                        type: "select",
                        labelIndx: "name",
                        valueIndx: "id",
                        prepend: {
                            "": ""
                        },
                        options: types
                    },
                    // validations: [{ type: 'minLen', value: "1", msg: "必填"}],
                    render: function (ui) {
                        var rowData = ui.rowData,
                            dataIndx = ui.dataIndx;

                        rowData.pq_cellcls = rowData.pq_cellcls || {};

                        let name = null;
                        $.each(types, function (index, obj) {
                            if (rowData[dataIndx] == obj.id) {
                                name = obj.name
                                return true;
                            }
                        });
                        return name;
                    }
                }
            ],
            postRenderInterval: -1, //call postRender synchronously.
            // pageModel: { type: "remote", rPP: 20 },
            create: function () {
                this.widget().pqTooltip();
            },
            dataModel: {
                data: option.initData
            }
        };

        grid = pq.grid(option.elem, obj);

        option.grid = grid;

        return new domain_design_index(option);
    }

    exports(MOD_NAME, new domain_design_index());
});